﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Web.Services.Protocols;

namespace SPWebShell.cmdlet
{
    [Cmdlet(VerbsCommon.Remove, "SPGroup")]
    public class RemoveGroup : CmdLetBase
    {
        [Parameter(Mandatory = true, HelpMessage = "The absolute URL to the SharePoint site collection. e.g. http://server/sites/SiteA/")]
        [ValidateNotNullOrEmpty]
        [Alias("siteurl")]
        public string Url { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The name of the SharePoint group which will be dropped")]
        [ValidateNotNullOrEmpty]
        [Alias("name", "groupname")]
        public string Group { get; set; }

        protected override void ProcessRecord()
        {
            try
            {
                VerboseLogger("Begin cmdlet Remove-SPGroup");
                VerboseLogger("Start time: " + DateTime.Now.ToString());
                ISPRepository repos = base.SPRepos;
                repos.RemoveGroup(this.Url, this.Group);
                VerboseLogger("End time: " + DateTime.Now.ToString());
                base.ProcessRecord();
            }
            catch (SoapException ex)
            {
                string message = ex.Detail.OuterXml;
                throw new Exception(message, ex);
            }
            finally
            {
                VerboseLogger("Exiting cmdlet Remove-SPGroup");
                WriteVerbose("End time: " + DateTime.Now.ToString());
            }
        }

    }
}
